package com.twu.biblioteca.view;


import com.twu.biblioteca.Customer;
import com.twu.biblioteca.Library;
import com.twu.biblioteca.session.SessionOption;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class ConsoleUI implements UI{

    private static final String ENTER_BOOK_TITLE_MESSAGE = "Enter the Book Title Followed by 'Enter' (not case sensitive)\n";
    private static final String ENTER_BOOK_AUTHOR_MESSAGE = "Enter the Book Author Followed by 'Enter' (not case sensitive)\n";
    private static final String ENTER_BOOK_YEAR_MESSAGE = "Enter the Book Year Followed by 'Enter' e.g 2014\n";
    private static final String ENTER_MOVIE_TITLE_MESSAGE = "Enter the Movie Title Followed by 'Enter' (not case sensitive)\n";
    private static final String ENTER_MOVIE_DIRECTOR_MESSAGE = "Enter the Movie Director Followed by 'Enter' (not case sensitive)\n";
    private static final String ENTER_MOVIE_YEAR_MESSAGE = "Enter the Movie Year Followed by 'Enter' e.g 2014\n";
    private static final String ENTER_PASSWORD_MESSAGE = "Enter Password\n";
    private static final String ENTER_LIBRARY_MESSAGE = "Enter Library Account Number\n";

    private static final String INVALID_LOGIN_MESSAGE = "Invalid Login\n";
    private static final String UNAVAILABLE_BOOK_MESSAGE = "That book is not available.\n";
    private static final String UNAVAILABLE_MOVIE_MESSAGE = "That movie is not available.\n";
    private static final String INVALID_BOOK_MESSAGE = "That is not a valid book to return.\n";
    private static final String INVALID_MOVIE_MESSAGE = "That is not a valid movie to return.\n";
    private static final String INVALID_OPTION_MESSAGE = "Select a valid option!\n";

    private static final String SUCCESS_BOOK_RETURN_MESSAGE = "Thank you for returning the book.\n";
    private static final String SUCCESS_CHECKOUT_MOVIE_MESSAGE = "Thank you! Enjoy the movie\n";
    private static final String SUCCESS_CHECKOUT_BOOK_MESSAGE = "Thank you! Enjoy the book\n";
    private static final String SUCCESS_LOGIN_MESSAGE = "Login Successful\n";
    private static final String SUCCESS_MOVIE_RETURN_MESSAGE = "Thank you for returning the movie.\n";

    private static final String WELCOME_MESSAGE = "Welcome to Biblioteca!\n";
    private static final String GOODBYE = "Goodbye\n";

    private final PrintStream output;
    private final Scanner inputScanner;

    public ConsoleUI(PrintStream output, InputStream input){
        this.output = output;
        this.inputScanner =  new Scanner(input);
    }

    public String[] readMovieDetails() {
        String[] args = new String[3];
        printOutput(ENTER_MOVIE_TITLE_MESSAGE);
        args[0] = readInput();
        printOutput(ENTER_MOVIE_DIRECTOR_MESSAGE);
        args[1] = readInput();
        printOutput(ENTER_MOVIE_YEAR_MESSAGE);
        args[2] = readInput();
        return args;
    }

    public String[] readLogin() {
        String[] args = new String[2];
        printOutput(ENTER_LIBRARY_MESSAGE);
        args[0] = readInput();
        printOutput(ENTER_PASSWORD_MESSAGE);
        args[1] = readInput();
        return args;
    }

    public String[] readBookDetails() {
        String[] args = new String[3];
        printOutput(ENTER_BOOK_TITLE_MESSAGE);
        args[0] = readInput();
        printOutput(ENTER_BOOK_AUTHOR_MESSAGE);
        args[1] = readInput();
        printOutput(ENTER_BOOK_YEAR_MESSAGE);
        args[2] = readInput();
        return args;
    }

    @Override
    public void successBookCheckout() {
        printOutput(SUCCESS_CHECKOUT_BOOK_MESSAGE);
    }

    @Override
    public void unavailableBook() {
        printOutput(UNAVAILABLE_BOOK_MESSAGE);
    }

    @Override
    public void successLogin() {
        printOutput(SUCCESS_LOGIN_MESSAGE);
    }

    @Override
    public void invalidLogin() {
        printOutput(INVALID_LOGIN_MESSAGE);
    }

    @Override
    public void welcomeMessage() {
        printOutput(WELCOME_MESSAGE);
    }

    @Override
    public void successCheckoutMovie() {
        printOutput(SUCCESS_CHECKOUT_MOVIE_MESSAGE);
    }

    @Override
    public void unavailableMovie() {
        printOutput(UNAVAILABLE_MOVIE_MESSAGE);
    }

    @Override
    public void listBooks(Library library) {
        printOutput(library.getBookListAsString());
    }

    @Override
    public void listMovies(Library library) {
        printOutput(library.getMovieListAsString());
    }

    @Override
    public void quit() {
        printOutput(GOODBYE);
    }

    @Override
    public void successBookReturn() {
        printOutput(SUCCESS_BOOK_RETURN_MESSAGE);
    }

    @Override
    public void invalidBookReturn() {
        printOutput(INVALID_BOOK_MESSAGE);
    }

    @Override
    public void successMovieReturn() {
        printOutput(SUCCESS_MOVIE_RETURN_MESSAGE);
    }

    @Override
    public void invalidMovieReturn() {
        printOutput(INVALID_MOVIE_MESSAGE);
    }

    @Override
    public void printCurrentlyLoggedIn(Customer currentlyLoggedIn) {
        printOutput(currentlyLoggedIn.toString());
    }

    public String readInput(){
        return inputScanner.nextLine();
    }

    @Override
    public void showOptions(boolean currentlyLoggedIn) {
        printOutput(SessionOption.getOptionListAsString(currentlyLoggedIn));
    }

    @Override
    public void invalidOptionEntered() {
        printOutput(INVALID_OPTION_MESSAGE);
    }

    private void printOutput(String message){
        output.print(message);
    }
}
